package org.yufan.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * Created by zxd on 2018/4/9.
 */
@Configuration
@PropertySource(value = { "classpath:jdbc.properties" }) // 读取properties配置文件
public class DataSourceConfig {


    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.driver}")
    private String jdbcDriver;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    //配置数据库连接池
    @Bean
    @ConditionalOnMissingBean(DataSource.class) //当容器里面没有指定的bean的情况下才创建
    public DataSource getDataSource(){
        DruidDataSource druidDataSource=new DruidDataSource();
        druidDataSource.setDriverClassName(jdbcDriver);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setUrl(url);
        druidDataSource.setMaxActive(20);
        druidDataSource.setMaxWait(60*1000);
        return druidDataSource;
    }




}
